<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="auto" v-show="showSearch">
            <el-form-item label="id">
                <el-input
                    v-model="queryParams.id"
                    placeholder="请输入id"
                    clearable
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="充值订单号">
                <el-input
                    v-model="queryParams.order_sn"
                    placeholder="请输入充值订单号"
                    clearable
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="充值金额">
                <el-input
                    v-model="queryParams.price"
                    placeholder="请输入充值金额"
                    clearable
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="支付状态">
                <el-select v-model="queryParams.pay_status"
                           clearable
                           placeholder="请选择">
                    <el-option
                        v-for="item in payStatusOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="parseInt(item.value)"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="支付类型">
                <el-select v-model="queryParams.pay_type"
                           clearable
                           placeholder="请选择">
                    <el-option
                        v-for="item in payTypeOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="parseInt(item.value)"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="&nbsp;">
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="Download"
                    @click="handleExport"
                >导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="list">
            <el-table-column label="ID" align="center" prop="id"/>
            <el-table-column label="充值订单号" align="center" prop="order_sn" :show-overflow-tooltip="true"/>
            <el-table-column label="充值金额" align="center" prop="price" :formatter="(row) => {return '￥' + row.price}"/>
            <el-table-column label="可退款" align="center" prop="refundable_price" :formatter="(row) => {return '￥' + (row.refundable_price || 0)}"/>
            <el-table-column label="已退款" align="center" prop="retreated_price" :formatter="(row) => {return '￥' + (row.retreated_price || 0)}"/>
            <el-table-column label="已消费" align="center" prop="consumption_amount" :formatter="(row) => {return '￥' + (row.consumption_amount || 0)}"/>
            <el-table-column label="支付状态" align="center" prop="pay_status">
                <template slot-scope="{row}">
                  <dict-tag :options="payStatusOptions" :value="row.pay_status" />
                </template>
            </el-table-column>
            <el-table-column label="支付类型" align="center" prop="pay_type">
                <template slot-scope="{row}">
                    <dict-tag :options="payTypeOptions" :value="row.pay_type" />
                </template>
            </el-table-column>
            <el-table-column label="昵称" align="center" prop="nikname" :show-overflow-tooltip="true"/>
            <el-table-column label="头像地址" align="center">
                <template slot-scope="{row}">
                    <el-image
                      v-if="row.avatar"
                      style="width: 40px; height: 40px"
                      :src="row.avatar"
                      :preview-src-list="[row.avatar]">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="create_time" width="180">
                <template slot-scope="{row}">
                    <span>{{ parseTime(row.create_time) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                    <el-button type="text" icon="el-icon-thumb" @click="showRefundDetail(row)">退款详情</el-button>
                    <el-button type="text" icon="el-icon-edit-outline" @click="showDeductionDetail(row)">扣费详情</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
        <el-dialog title="退款详情" key="dialog1" :visible.sync="refundDetailDialogVisible" width="60%" append-to-body>
            <RefundDetail v-if="refundDetailDialogVisible" :key="'RefundDetail' + curr_id" :recharge_id="curr_id" />
        </el-dialog>
        <el-dialog title="扣费详情" key="dialog2" :visible.sync="deductionDetailDialogVisible" width="60%" append-to-body>
            <RefundDetail v-if="deductionDetailDialogVisible" :key="'RefundDetail1' + curr_id" :recharge_id="curr_id" />
        </el-dialog>
    </div>


</template>

<script >
import * as api from "@/api/finance/recharge";
import RefundDetail from "@/components/Common/user/components/RefundDetail.vue";
export default {
    name: 'RechargeRecord',
    components: { RefundDetail },
    props: {
      uid: {
        type: Number,
        required: true
      },
    },
    data() {
      return {
        list:[],
        loading:true,
        showSearch:false,
        total:0,
        payStatusOptions:[
          { label: '未支付', value: '0' },
          { label: '已支付', value: '1' },
        ],
        payTypeOptions:[
          { label: '微信支付', value: '1' },
          { label: '支付宝支付', value: '2' },
          { label: '后台充值', value: '3' },
        ],
        curr_id:0,
        refundDetailDialogVisible:false,
        deductionDetailDialogVisible:false,
        form: {},
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        rules: {},
      };
    },
    mounted(){
      this.queryParams.userid = this.uid
      this.getList()
    },
    methods: {
      getList() {/** 查询列表 */
        this.loading = true;
        api.listRecharge(this.queryParams).then(response => {
            this.list = response.rows || [];
            this.total = response.total || 0;
            this.loading = false;
        });
      },
      handleQuery() {/** 搜索按钮操作 */
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {/** 重置按钮操作 */
        this.dateRange = [];
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
        };
        this.resetForm("queryRef");
        this.handleQuery();
      },
      handleExport() {/** 导出按钮操作 */
        this.download(api.EXPORT_URL, {
            ...this.queryParams.value
        }, `recharge_${new Date().getTime()}.xlsx`);
      },
      showRefundDetail(row) {
        this.curr_id = row.id
        this.refundDetailDialogVisible = true
      },
      showDeductionDetail(row) {
        this.curr_id = row.id
        this.deductionDetailDialogVisible = true
      },
    }
  }
</script>
